﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace Com
{
    public static class StringEx
    {
        public static string ToJson(this object obj)
        {
            var jsonString = JsonConvert.SerializeObject(obj);
            const string p = @"\\/Date\((\d+)\)\\/";
            var matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
            var reg = new Regex(p);
            jsonString = reg.Replace(jsonString, matchEvaluator);
            return jsonString;
        }
        private static string FormateStr
        {
            get;
            set;
        }
        private static string ConvertJsonDateToDateString(Match m)
        {
            var dt = new DateTime(1970, 1, 1);
            dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
            dt = dt.ToLocalTime();
            var result = dt.ToString(FormateStr);
            return result;
        }
    }
}
